package pers.lls.im.core.task;

import pers.lls.im.core.api.MessagePersistService;
import pers.lls.im.core.common.bean.ChatMessage;
import pers.lls.im.core.common.pool.DefaultThreadFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @author lihuan
 * @version V1.0 创建时间：2019/8/02
 * Copyright 2019 by landray & STIC
 */
@Slf4j
//@Component
public class DataAsynchronousTask {

    @Autowired
    MessagePersistService messagePersistService;

    ExecutorService executorService = new ThreadPoolExecutor(100, 100,
            60L, TimeUnit.SECONDS,
            new ArrayBlockingQueue(100),new DefaultThreadFactory("im"));

    public void writeData(ChatMessage chatMessage) {
        Boolean result = false;
        final FutureTask<Boolean> future = new FutureTask<>(new DataCallable(messagePersistService, chatMessage));
        executorService.execute(future);
        try {
            result = future.get();
        } catch (Exception e) {
            log.error("[数据写入异常 {}", e.getMessage());
        }
        if (!result) {
            log.error("任务执行异常 !");
        }
    }

}
